package com.eascs.workflow.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eascs.admin.o2o.backfee.entity.O2OComBackFee;
import com.eascs.admin.o2o.backfee.service.O2OComBankFeeService;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.entity.OaDeposit;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.service.OaLoanapplyService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.overdue.entity.O2oStatus;

/**
 * 划拨保证金 构造实体
 * @author ycz
 *
 */
@Component
public class TranDepositSevice {
	private static Logger logger = LoggerFactory.getLogger(TranDepositSevice.class);
	@Autowired
	private OaLoanapplyService oaLoanapplyService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Autowired
	private O2OComBankFeeService o2oComBankFeeService;
	/**
	 * 返回可以划拨编号列表
	 * @param idCard
	 * @param billCodeList
	 * @return
	 */
	public List<OaDeposit> findBillCode(String idCard,O2oLoanapplyEntity loanapply){
		List<OaDeposit> billCodeList=new ArrayList<>();
		List<LoanapplyExpand> expandList=loanapplyExpandService.findLoanId(loanapply.getLoanId());
		if(null!=expandList && !expandList.isEmpty()){
			for (LoanapplyExpand loanapplyExpand : expandList) {
				if(compareToZero(loanapplyExpand.getRedDeposite(),loanapplyExpand.getRetaDeposite())){
					O2OComBackFee comBackFee = o2oComBankFeeService.checkBackStatus(loanapply.getLoanapplyNo());
					if (null == comBackFee) {
						if(queryOverDue(loanapplyExpand.getBillCode())){
							logger.info("新系统可退保证金单号[{}]：", loanapplyExpand.getBillCode());
							OaDeposit oaDeposit=new OaDeposit();
							oaDeposit.setBillCode(loanapplyExpand.getBillCode());
							oaDeposit.setLoanapplyNo(loanapplyExpand.getLoanapply_no());
							oaDeposit.setTranDeposit(String.valueOf(subtractToZero(loanapplyExpand.getRedDeposite(),loanapplyExpand.getRetaDeposite())));
							billCodeList.add(oaDeposit);
						}
					}
				}
			}
		}
		return billCodeList;
	}
	/**
	 * 判断新老系统单号是否有逾期
	 * @param billCode
	 * @return
	 */
	private Boolean queryOverDue(String billCode){
		List<O2oStatus> o2oStatusList = oaLoanapplyService.findOverdueBillCode(billCode);
		if (!o2oStatusList.isEmpty()) {
			List<String> strList = new ArrayList<>();
			for (int j = 0; j < o2oStatusList.size(); j++) {
				strList.add(o2oStatusList.get(j).getStatus());
			}
			if (!strList.contains(StatusContexts.O2O_FIVE)) {
				return false;
			}
		}
		return true;
	}
	/**
	 * BigDecimal 为空值转成0
	 * @param decimal
	 * @return
	 */
	public Boolean compareToZero(BigDecimal redDeposit,BigDecimal retaDeposit){
		if(redDeposit==null){
			redDeposit=BigDecimal.ZERO;
		}
		if(retaDeposit==null){
			retaDeposit=BigDecimal.ZERO;
		}
		Boolean flag=true;
		BigDecimal count=redDeposit.subtract(retaDeposit);
		if(count.compareTo(BigDecimal.ZERO)==-1 || count.compareTo(BigDecimal.ZERO)==0 ){
			flag=false;
		}
		return flag;
	}
	/**
	 * 计算可退金额
	 * @param redDeposit
	 * @param retaDeposit
	 * @return
	 */
	public BigDecimal subtractToZero(BigDecimal redDeposit,BigDecimal retaDeposit){
		if(redDeposit==null){
			redDeposit=BigDecimal.ZERO;
		}
		if(retaDeposit==null){
			retaDeposit=BigDecimal.ZERO;
		}
		return redDeposit.subtract(retaDeposit);
	}
}
